package com.atguigu.thread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

public class BlockingQueueDemo {
    public static void main(String[] args) throws InterruptedException {

        //创建一个阻塞队列，设定队列长度为3
        BlockingQueue<String> queue = new ArrayBlockingQueue<>(3);

        //四种组合测试

       /* //第一组方法 add remove element
        System.out.println(queue.add("a"));
        System.out.println(queue.add("b"));
        System.out.println(queue.add("c"));
        //System.out.println(queue.add("d"));

        System.out.println(queue.element()); //获取第一个值不删除

        System.out.println(queue.remove());  //获取第一个值并删除
        //System.out.println(queue.element());
        //System.out.println(queue.remove());
        //System.out.println(queue.remove());
        //System.out.println(queue.remove());*/

        /*//第二组：offer peek poll
        System.out.println(queue.offer("a"));
        System.out.println(queue.offer("b"));
        System.out.println(queue.offer("c"));
        //System.out.println(queue.offer("d")); //超出队列长度会返回false

        System.out.println(queue.peek());  //获取第一个元素，元素为空返回null

        System.out.println(queue.poll());  //出队第一个元素，队列为空返回null
        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll());*/

       /* //第三组：put take
        queue.put("a");
        queue.put("b");
        queue.put("c");
        //queue.put("d");  //入队，超过队列长度阻塞
        System.out.println(queue.take());
        //System.out.println(queue.take());
        //System.out.println(queue.take());
        //System.out.println(queue.take());  //出队第一个元素，队空发生阻塞*/

        //第四组：offer poll
        System.out.println(queue.offer("a", 3, TimeUnit.SECONDS));
        System.out.println(queue.offer("b", 3, TimeUnit.SECONDS));
        System.out.println(queue.offer("c", 3, TimeUnit.SECONDS));
        System.out.println(queue.offer("d", 3, TimeUnit.SECONDS));

        System.out.println(queue.element());
        System.out.println(queue.peek());

        System.out.println(queue.poll(3, TimeUnit.SECONDS));
        System.out.println(queue.poll(3, TimeUnit.SECONDS));
        System.out.println(queue.poll(3, TimeUnit.SECONDS));
        System.out.println(queue.poll(3, TimeUnit.SECONDS)); //队空仍然出队，超时返回null

    }
}
